package com.cssw.fyzb.busi.api.pay;

import com.alibaba.fastjson.JSON;
import com.cssw.fyzb.busi.config.TpsClient;
import com.cssw.fyzb.busi.service.acct.UserCashOutService;
import com.cssw.fyzb.busi.service.pay.PayService;
import com.cssw.fyzb.busi.util.MapUtil;
import com.cssw.fyzb.core.exception.BizException;
import com.cssw.fyzb.core.model.dodb.UserCashOut;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Author: yangyiqun
 * @Date: 2020/7/14 22:10
 */

@Function(description = "ada提现异步通知校验", parameters = {
        @Parameter(name = "params", type = Type.String, description = "参数", required = true),
})
@Component
public class AdapayDrawcashNotify implements Action {

    private static final Logger logger = LoggerFactory.getLogger(AdapayDrawcashNotify.class);
    @Autowired
    private TpsClient tpsClient;

    @Autowired
    private UserCashOutService userCashOutService;

    @Override
    public Response execute(Request request) throws Exception {
        String params = request.getString("params");
        logger.info(" params >>> " + params);

        Map param = JSON.parseObject(params, Map.class);
        String id = MapUtil.getString(param, "id");
        String status =  MapUtil.getString(param, "status");

        userCashOutService.cashOutYConnCallback(id, "succeeded".equals(status) ? UserCashOut.STATUS_SUCC : UserCashOut.STATUS_FAIL);

        return Response.success();
    }


    private Map orderQuery(String tradeNo) {
        Request request = new Request("QueryAdapay");
        request.setParameter("tradeNo", tradeNo);
        Response response = tpsClient.send(request);
        if (!response.isSuccess()) {
            throw new BizException(response.getCode(), response.getMessage());
        }
        return response.getData().get("data", Map.class);

    }
}